
(function(hy) {
	var $window = hy.$window,
	$document = hy.$document,
	$ = hy.$,
	htmlz = hy.htmlz;

    var message = function(message){
        
    };

    message.method = {
        initialize: function(message, options) {
			if (!message) return;
			var defaults = {
				type: "info", // 类型: info - 消息, success - 成功, warning - 警告, error - 错误, confirm - 确认
				modal: false, // 模态
				shadow_bgcolor: "#666", // 阴影背景色
				shadow_opacity: 0.9, // 阴影透明度
				show_close: true, // 显示关闭按钮
				auto_close: false, // 自动关闭
				animation: "fade up", // 显示动画效果
				onClose: function(){}, // 关闭事件
				onConfirm: function(b){} // 确认对话框回调事件 
			};
			options = options || {};
			options = $.extend(defaults, options);
			this.$$options = options;
			this.dialog(message, options);
		},
		dialog: function(message, options) {
			var confirm = options["type"] == "confirm";
			var type = confirm ? "warning" : options["type"];
			// 对话框主体
			this.$dialog = $(htmlz("div.ui.compact.{0}.message{position:fixed;z-index:1001;padding-right:25px;display:none;}".replace(/\{\d+\}/i, type)));
			// 关闭按钮
			if (options["show_close"]) {
				this.$close = $(htmlz("i.close.icon")).appendTo(this.$dialog);
			}
			// 标题
			if (options["title"]) {
				this.$title = $(htmlz("div.header")).append(options["title"]).appendTo(this.$dialog);
			}
			//消息内容
			this.$message = $(htmlz("div.content")).html(message).appendTo(this.$dialog);
			// 确认对话 type="confirm"
			if (confirm) {
				this.$actions = $(htmlz("div.actions {text-align:center;margin-top:10px;}")).appendTo(this.$dialog);
				this.$confirm = $(htmlz("a.ui.green.label")).append("确认").appendTo(this.$actions);
				this.$cancel = $(htmlz("a.ui.red.label")).append("取消").appendTo(this.$actions);
				this.$dialog.css("min-width","200px");
			}
			// 是否模态
			(options["modal"]) && this.shadow(options);

			this.$dialog.appendTo(document.body);
			// 事件绑定
			this.event();
			// 位置
			this.position(options["position"]);
			// 显示
			this.$dialog.transition(options["animation"]);
			// 自动关闭
			if (options["auto_close"]) {
				var self = this;
				setTimeout(function(){
					self.destroy();
				},3000);
			}
		},
		shadow: function(options) {
			var str = "{background:"+options["shadow_bgcolor"]+";z-index:1000;width:100%;height:100%;position: fixed; left: 0px; top: 0px; opacity: "+options["shadow_opacity"]+";filter:Alpha(Opacity=" + options["shadow_opacity"] * 100 + ");}";
			this.$shadow = $(htmlz("div" + str)).appendTo(document.body);
		},
		event: function() {
			var self = this, options = self.$$options;
			// 关闭按钮事件
			if (self.$close) {
				self.$close.bind("click",function(e){
					options["onClose"].call(self);
					self.destroy();
					return;
				});
			}
			// 确认对话框按钮事件
			if (this.$actions) {
				this.$confirm.bind("click", function(e){
					options["onConfirm"].call(self, true);
					self.destroy();
					return true;
				});
				this.$cancel.bind("click", function(e){
					options["onConfirm"].call(self, false);
					self.destroy();
					return false;
				});
			}
			// 非模态时页面鼠标按下事件
			if (!options["modal"] && !options["auto_close"]) {
				$(document).bind("mousedown.message", function(e){
					var p = $(e.target).closest("div.ui.compact.message");
					(!p.length) && self.destroy();
				});
			}
		},
		position: function(position) {
			var css = {};
			if (!position) {
				var w = this.$dialog.outerWidth(true) / 2;
				var h = this.$dialog.outerHeight(true) / 2;
				css = {
					top: "50%",
					left: "50%",
					margin: (-h) + "px 0px 0px " + (-w) + "px"
				};
			} else {
				for (var i in position) {
					var prop = position[i].split(":");
					if (/(top|right|bottom|left)/i.test(prop[0]) && /\d+/i.test(prop[1])){
						css[prop[0]] = prop[1] + "px";
					}
					css["margin"] = "0px";
				}
			}
			this.$dialog.css(css);
		},
		destroy: function() {
			// DOM 移除
			(this.$dialog) && this.$dialog.remove();
			(this.$shadow) && this.$shadow.remove();
			// event move
			$(document).unbind(".message");
			this.$dialog = null;
			this.$close = null;
			this.$title = null;
			this.$message = null;
			this.$actions = null;
			this.$shadow = null;
			this.$confirm = null;
			this.$cancel = null;
			this.$$options = null;
		}
	};

    };

    hy.message = message;
})(hy);
